package com.recsmile.sgs.ui;

import com.recsmile.sgs.core.Player;
import com.recsmile.sgs.core.net.codec.ProtoBufDecoder;
import com.recsmile.sgs.core.net.codec.ProtoBufEncoder;
import com.recsmile.sgs.entity.ClientRoom;
import com.recsmile.sgs.entity.GamePanel;
import com.recsmile.sgs.listener.PacketListener;
import com.recsmile.sgs.net.codec.CommandChannelHandler;
import com.recsmile.sgs.ui.components.GamePanelGui;
import com.recsmile.sgs.ui.components.LobbyGui;
import com.recsmile.sgs.ui.components.RoomGui;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * Created by Administrator on 2016/8/25.
 */
public class MainFrame implements ActionListener, PacketListener{

    private Channel channel;

    private JFrame frame;

    private JPanel mainPanel;

    public MainFrame(){
        frame = new JFrame("Sanguosha");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(0, 0);
        frame.setSize(1000,700);
        frame.setVisible(true);
        //frame.pack();

        //this.startConnet();

        initJMenu();

        //setMainPanel(new LobbyGui(Arrays.asList(new ClientRoom(), new ClientRoom()), null));
        //setMainPanel(new RoomGui(new ClientRoom(),channel));
        List<Player> players = new ArrayList<>();
        for(int i = 0; i < 8;i++){
            Player player = new Player();
            player.setId(100 + i);
            player.setLocation(0);
            player.setNickname("nickName:"+ player.getId());
            player.setOpenid("openid:"+ player.getId());
            players.add(player);
        }

        setMainPanel(new GamePanel(players.get(0),players).getGamePanelGui());
    }

    private void startConnet() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Bootstrap bootstrap = new Bootstrap();
                bootstrap.group(new NioEventLoopGroup(1)).channel(NioSocketChannel.class)
                        .handler(new ChannelInitializer<SocketChannel>() {
                            protected void initChannel(SocketChannel ch) throws Exception {
                                ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,
                                                0,4,0,4),
                                        new ProtoBufDecoder(),
                                        new ProtoBufEncoder(),
                                        new CommandChannelHandler(MainFrame.this)
                                );
                            }
                        }).option(ChannelOption.SO_KEEPALIVE,true);
                try {
                    channel = bootstrap.connect("127.0.0.1",8899).sync().channel();
                    channel.closeFuture().sync();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }



    private JMenuBar jMenuBar;
    private JMenu newGame;
    private JMenuItem enter;

    private void initJMenu(){
        jMenuBar = new JMenuBar();
        enter = new JMenuItem("enter");
        enter.addActionListener(this);
        newGame = new JMenu("NewGame");
        newGame.add(enter);
        jMenuBar.add(newGame);

        frame.setJMenuBar(jMenuBar);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == enter){

        }
    }

    public void setMainPanel(JPanel jPanel){
        if(this.mainPanel != null){
            this.frame.remove(this.mainPanel);
        }
        this.mainPanel = jPanel;
        this.frame.add(this.mainPanel);
    }
}
